package com.njwb;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/mybatisplus?serverTimezone=UTC";
        String userName = "root";
        String pwd = "root";
        FastAutoGenerator.create(url, userName, pwd)
                .globalConfig(builder -> builder
                        .author("njwb")//作者
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")//java代码生成路径
                        .commentDate("yyyy-MM-dd")//创建日期
                )
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.njwb") // 设置父包名
                            .moduleName("mp") // 设置父包模块名
                            .entity("pojo")//修改实体包
                            .mapper("mapper")//修改mapper包
                            .service("service")
                            .serviceImpl("service1.impl")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, Paths.get(System.getProperty("user.dir"))
                                                               + "/src/main/resources/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> builder
                        .addInclude("dept","t_log")//设置需要生成的表名
                        .addTablePrefix("t_").addFieldPrefix("t_")// 设置过滤前缀
                        .entityBuilder()
                        .enableLombok()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
